package FanXingT;

import com.google.gson.Gson;

import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Date;

/**
 * @author Ajie
 * @date 2022/2/25
 * @function
 * https://blog.csdn.net/u011652925/article/details/78667047
 *
 */
public class GetTMethod {
    public static void main(String[] args) {

        User user = new User();
        user.age = 18;
        user.name = "aJie";
//        Object test = test(new Gson().toJson(user), User.class);
        Object test = test(new Gson().toJson(user), new User());
        User test1 = (User) test;
        System.out.println(test1.age+"  "+test1.name);
    }

//    public static <T> void test(String data,Object obj){
    public static <T> T test(String data,Object obj){
//    public static <T> T test(String data,Class<T> t){

//        System.out.println(new Gson().toJson());
//        System.out.println(classes[0].getName());
//        Type aClass = obj.getClass();
//
//        System.out.println(aClass.getTypeName());

        //调用
//        Class<T> typeof = (Class<T>) getTClass(t);
//        System.out.println("typeof:"+typeof);
//        return new Gson().fromJson(data, typeof);
//        return new Gson().fromJson(data, t);
        return null;
    }

    //具体获取泛型类型的方法
    public static <T> Class getTClass(T t) {
        Type[] params = t.getClass().getGenericInterfaces();
        Type type = params[0];
        Type finalNeedType; 
        if (params.length > 1) {
            if (!(type instanceof ParameterizedType)) throw new IllegalStateException("没有填写泛型参数");
            finalNeedType = ((ParameterizedType) type).getActualTypeArguments()[0];
        } else {
            finalNeedType = type;
        }
        final Class clazz = getClass(finalNeedType, 0);
        return clazz;
    }

    //方法1
    public static Class getGenericClass(ParameterizedType parameterizedType, int i) {
        Object genericClass = parameterizedType.getActualTypeArguments()[i];
        if (genericClass instanceof ParameterizedType) {
            return (Class) ((ParameterizedType) genericClass).getRawType();
        } else if (genericClass instanceof GenericArrayType) {
            return (Class) ((GenericArrayType) genericClass).getGenericComponentType();
        } else if (genericClass instanceof TypeVariable) {
            return (Class) getClass(((TypeVariable) genericClass).getBounds()[0], 0);
        } else {
            return (Class) genericClass;
        }
    }

    //方法 2
    public static Class getClass(Type type, int i) {
        if (type instanceof ParameterizedType) {
            return getGenericClass((ParameterizedType) type, i);
        } else if (type instanceof TypeVariable) {
            return (Class) getClass(((TypeVariable) type).getBounds()[0], 0);
        } else {
            return (Class) type;
        }
    }


    //补充一个是否为java基础类型的类
    public static boolean isBaseDataType(Class<?> clazz) {
        return clazz.isPrimitive() || clazz.equals(String.class) || clazz.equals(Boolean.class)
                || clazz.equals(Integer.class) || clazz.equals(Long.class) || clazz.equals(Float.class)
                || clazz.equals(Double.class) || clazz.equals(Byte.class) || clazz.equals(Character.class)
                || clazz.equals(Short.class) || clazz.equals(Date.class) || clazz.equals(byte[].class)
                || clazz.equals(Byte[].class);
    }

}
